﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json.Linq;
using FBEntities.FBObjects.User;
using FBEntities.FBObjects;

namespace FBEntities.FBConnections
{
    public interface IFBConnection
    {
        void Parse(string senderId);
    }

    public class FBConnection : IFBConnection
    {
        public bool Parsed;
        internal string type = "";

        public FBPaging paging;

        #region IFBConnection Members

        public void Parse(string senderId)
        {
            string url = "https://graph.facebook.com/" + senderId + "/" + type + "?" + FBParseUtilities.authKey;

            string json = FBCache.GetJSON(type + "-" + senderId, url);
            if (string.IsNullOrEmpty(json))
                return;

            JObject o = JObject.Parse(json);

            Parse(o);
        }

        public void ParseCached(string senderId)
        {
            string json = FBCache.GetCachedJSON(type + "-" + senderId);
            if (string.IsNullOrEmpty(json))
                return;

            JObject o = JObject.Parse(json);

            Parse(o);
        }

        private void Parse(JObject o)
        {
            FBParseUtilities.Parse(o, this);
            Parsed = true;
        }
        #endregion
    }
}
